// TODO: CODE REVIEW & CLEANUP!
//
// ToolsScreen.cs
//
// Implements ToolsScreen and related types.
//

using System;
using System.Windows;
using System.Windows.Controls;
using HomeUX.ClientControlSupport;
using HomeUX.UiUtilities;

namespace HomeUX.ClientAppSupport
{

public partial class ToolsScreen : UserControl
{
    Screen _screen;
   
    public ToolsScreen()
	{
		// Required to initialize variables
		InitializeComponent();

        // hook up events
        Loaded += ToolsScreen_Loaded;
        HomeTabDropDownList.SelectedItemValueChanged += HomeTabDropDownList_SelectedItemValueChanged;
        Application.Current.Host.Content.FullScreenChanged += OnFullScreenChanged;
    }

    void HomeTabDropDownList_SelectedItemValueChanged(object sender, EventArgs e)
    {
        if (ClientFramework.Current.IsInitialized)
            ClientFramework.Current.HomeScreenName = (string) HomeTabDropDownList.SelectedItemValue;
    }

    void ToolsScreen_Loaded(object sender, RoutedEventArgs e)
    {
        _screen = ClientFramework.Current.TryFindScreen(this);
        _screen.Opened += _screen_Opened;

        // enable the "reload" function if it's available
        bool enableReload = ClientFramework.Current.CanReload;
        ReloadButton.IsEnabled = enableReload;
        UiUtil.EnableTextBlock(ReloadLabel, enableReload);

        // "simulated kiosk mode" is 1:1 equivalent to Silverlight full-screen mode; when this
        // application runs within an HTA (in regular touch screen mode, also known as
        // ClientGlobals.HostKioskMode), "simulated kiosk mode" is not available
        SimulatedKioskModeButton.IsLit = Application.Current.Host.Content.IsFullScreen;
        UiUtil.EnableTextBlock(SimulatedKioskModeLabel, !ClientFramework.Current.IsHostKioskMode);
        SimulatedKioskModeButton.IsEnabled = !ClientFramework.Current.IsHostKioskMode;

        foreach (Screen screen in ClientFramework.Current.Screens.Values)
        {
            if ((screen.Label.Length > 0) && !screen.DisableDirectNavigation)
            {
                HomeTabDropDownList.Items.Add(new TextBlock()
                {
                    Text = screen.Label,
                    Tag = screen.Name
                });
            }
        }
        HomeTabDropDownList.SelectedItemValue = ClientFramework.Current.HomeScreenName;
    }

    void _screen_Opened(object sender, EventArgs e)
    {
        GC.Collect();
        long bytesAllocated = GC.GetTotalMemory(true);
        MemoryStatusTextBlock.Text = String.Format("{0:n0} bytes of memory in use", bytesAllocated);
    }

    private void SimulatedKioskModeButton_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Host.Content.IsFullScreen =
            !Application.Current.Host.Content.IsFullScreen;
    }

    void OnFullScreenChanged(object sender, EventArgs e)
    {
        bool simulatedKioskMode = Application.Current.Host.Content.IsFullScreen;
        SimulatedKioskModeButton.IsLit = simulatedKioskMode;
        ClientGlobals.IsTouchMode = simulatedKioskMode;
    }

    private void ConnectionSettingsButton_Click(object sender, RoutedEventArgs e)
    {
        ClientFramework.Current.GoToScreen(ClientFramework.ConnectionSettingsScreenName, null);
    }

    private void ReloadButton_Click(object sender, RoutedEventArgs e)
    {
        ClientFramework.Current.Reload();
    }
}

}
